
package com.ccsoft.atools.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import com.ccsoft.atools.dao.AssBaseSimpleMapper;
import com.ccsoft.atools.model.AssBaseSimple;
import com.ccsoft.atools.utils.BusinessException;
import com.ccsoft.atools.utils.Example;
import java.util.Optional;

/**
 * @Description : 服务层实现类，由代码生成器自动生成
 * @Author : chanchaw
 * @Date : 2023-4-3 13:21:19
 */
@Service
public class AssBaseSimpleServiceImpl implements AssBaseSimpleService {
    @Autowired
    private AssBaseSimpleMapper dao;
    @Autowired
    private AssBaseSimpleService meService;

    @Override
    @Transactional
    public AssBaseSimple insert(AssBaseSimple record) {
        record = meService.fullFill(record);
        meService.doBeforeInsert(record);
        dao.insert(record);
        meService.doAfterInsert(record);
        return record;
    }

    @Override
    @Transactional
    public AssBaseSimple insertSelective(AssBaseSimple record) {
        record = meService.fullFill(record);
        meService.doBeforeInsert(record);
        dao.insertSelective(record);
        meService.doAfterInsert(record);
        return record;
    }

    /* 由于本方法无法返回所有对象的自增主键，后者调整为一个一个保存的方法
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<AssBaseSimple> list) {
        return dao.multiInsert(list);
    }
     */
    @Override
    @Transactional
    public List<AssBaseSimple> multiInsert(List<AssBaseSimple> list) {
        list.forEach(item -> meService.insertSelective(item));
        return list;
    }

    @Override
    public int updateByPrimaryKey(AssBaseSimple record) {
        return dao.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(AssBaseSimple record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer iid) {
        return dao.deleteByPrimaryKey(iid);
    }

    @Override
    public AssBaseSimple selectByPrimaryKey(Integer iid) {
        return dao.selectByPrimaryKey(iid);
    }

    @Override
    public List<AssBaseSimple> selectAll(){
        return dao.selectAll();
    }

    @Override
    public AssBaseSimple save(AssBaseSimple record){
        if(record == null) throw new BusinessException("空对象不可保存！");
        Integer iid = Optional.ofNullable(record.getIid()).orElse(0);
        if(iid==0) insertSelective(record);
        else updateByPrimaryKeySelective(record);
        return record;
    }

    // 没有使用 xml 中的 multiSave 方法是因为无法批量回写自增主键
    @Transactional
    @Override
    public List<AssBaseSimple> multiSave(List<AssBaseSimple> list){
        if(list == null || list.size()==0) return list;
        list.forEach(item -> meService.save(item));
        return list;
    }

    /**=========================下面开始是固定模式代码=======================================*/
    // 新增前检测，不可新增则抛出异常
    private void check4Insert(AssBaseSimple record){
    }

    @Override
    public AssBaseSimple fullFill(AssBaseSimple record) {
        // TODO: 默认前端传递来的参数不需要填充完整，如果需要则使用自定义逻辑覆盖下面的代码
        return AssBaseSimpleService.super.fullFill(record);
    }

    @Override
    public void doBeforeInsert(AssBaseSimple record) {
        // TODO: 在新增数据前需要做的逻辑写在这里，抛出异常则终止新增操作
        AssBaseSimpleService.super.doBeforeInsert(record);
    }

    @Override
    public void doAfterInsert(AssBaseSimple record) {
        // TODO: 新增数据后需要做的逻辑写在这里，抛出异常则终止新增操作
        AssBaseSimpleService.super.doAfterInsert(record);
    }

    @Override
    public List<AssBaseSimple> selectByExample(Example<AssBaseSimple> example){
        return dao.selectByExample(example);
    }
    //=================以上是自动生成的代码，自定义代码请写在下面==================

    @Override
    public List<AssBaseSimple> getByCategory(String category){
        return selectByExample(new Example().andEq("category", category));
    }
}